<?php
/**
 * crcl/base
 *
 * Copyright 2013 CRCL - André Sabosch - info@crcl.de
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

declare(encoding = 'UTF-8');
namespace crcl\base\template\instructions;

interface IConcrete
{
  /**
   * Compiles instruction
   * You can fetch results with @see getOutput
   */
  public function execute();

  /**
   * Verifies instruction
   */
  public function verify();

  /**
   * Sets attributes
   *
   * @param $aAttributes  array
   */
  public function setAttributes($aAttributes);

  /**
   * Returns output, defaults to empty string
   *
   * @return string
   */
  public function getOutput();
}

abstract class Base implements IConcrete
{
  public function setAttributes($aAttributes)
  {
    $this->aAttributes = $aAttributes;
  }

  public function setValues($aValues)
  {
    $this->aValues = $aValues;
  }

  public function getOutput()
  {
    return $this->sOutput;
  }

  /** PROTECTED ***********************************************************************************/

  protected $sOutput = '';
  protected $aAttributes = array();
  protected $aValues = array();

  protected function getAttribute($sAttribute)
  {
    return $this->aAttributes[$sAttribute];
  }

  protected function getValue($sKey)
  {
    if (isset($this->aValues[$sKey]))
    {
      $sValue = $this->aValues[$sKey];
    }
    else
    {
      throw new Exception(Exception::ERROR_INSTRUCTIONS_UNKNOWN_VALUE,
        array('value' => $sKey));
    }
    return $sValue;
  }
}